﻿<%
'---------------------------------------------------------------------------
' Title        : IP Address to Country Lookup Service using Database
' Requirements : ASP 2.0+ and MS-ACCESS
' Installation : a. Copy this asp file (ipcountry.asp) and mdb database file (ipcountry.mdb)
'                   into a web directory.
'                b. Browse this webpage (ipcountry.asp) using http protocol.
'                   Example: http://localhost/demo/ipcountry.asp
'                c. Enter an IP address range and click submit.
'
' Author       : Jgsoft Associates
' URL          : http://www.ip2country.net/ip2country/ip_country.asp
' Email        : sales@ip2country.net
'
' Copyright (c) 2004 by Jgsoft Associates
'---------------------------------------------------------------------------


Public Function IP2Country(ByVal ipaddress) 
    Dim conn_ip, rs, sql, accessdb, strconn
    Dim ipno,ipcountry

    ipcountry=""
    ipno = Dot2LongIP(ipaddress)
    ' select MS-Access database using DSNless connection	

    dbName=server.MapPath("ipcountry/ipcountry.mdb")	
    Set conn_ip=Server.CreateObject("adodb.connection") 
    conn_ip.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & dbName
            
    ' query string to lookup the country by matching the range of IP address number
    strsql = "SELECT * FROM ip2country WHERE " & ipno & " BETWEEN begin_num AND end_num"
            
    ' execute the query
    Set rs = conn_ip.execute(strsql)

    If Not rs.EOF Then
        ipcountry = rs("country")
    End If

    rs.close
    Set rs = Nothing
    conn_ip.close
    Set conn_ip = Nothing
  
  IP2Country= ipcountry
End function 


' Convert dotted IP address into IP number in long
Function Dot2LongIP (ByVal DottedIP)
    Dot2LongIP = 0

    'Dim i, pos
    'Dim PrevPos, num
    'If DottedIP = "" Then
    '    Dot2LongIP = 0
    'Else
    '    For i = 1 To 4
    '        pos = InStr(PrevPos + 1, DottedIP, ".", 1)
    '       If i = 4 Then 
    '            pos = Len(DottedIP) + 1
    '        End If
    '        num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
    '        PrevPos = pos
    '        Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) +   Dot2LongIP
    '    Next
    'End If
End Function

'Get the real IP address of the user
Public Function GetClientIP() 
    Dim uIpAddr 
    uIpAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR") 
    If uIpAddr = "" Then
        uIpAddr = Request.ServerVariables("REMOTE_ADDR") 
    End If
    GetClientIP = uIpAddr 
    uIpAddr = "" 
End function 

Function CountryName(sAbb)
sAbb = UCASE(sAbb)  'Caps
sLetter = Left(sAbb,1)
If sLetter = "A" Then
    Select Case sAbb
        Case "AE"
        CountryName = "United Arab Emirates"
        Case "AF"
        CountryName = "Afghanistan"
        Case "AL"
        CountryName = "Albania"
        Case "AS"
        CountryName = "American Samoa"
        Case "AD"
        CountryName = "Andorra"
        Case "AO"
        CountryName = "Angola"
        Case "AI"
        CountryName = "Anguilla"
        Case "AQ"
        CountryName = "Antarctica"
        Case "AG"
        CountryName = "Antigua and Barbuda"
        Case "AR"
        CountryName = "Argentina"
        Case "AN"  '#
        CountryName = "Netherlands Antilles"
        Case "AM"
        CountryName = "Armenia"
        Case "AW"
        CountryName = "Aruba"
        Case "AU"
        CountryName = "Australia"
        Case "AT"
        CountryName = "Austria"
        Case "AZ"
        CountryName = "Azerbaijan"
        End Select
ElseIf sLetter = "B" Then
        Select Case sAbb
        Case "BS"
        CountryName = "Bahamas"
        Case "BH"
        CountryName = "Bahrain"
        Case "BD"
        CountryName = "Bangladesh"
        Case "BB"
        CountryName = "Barbados"
        Case "BY"
        CountryName = "Belarus"
        Case "BE"
        CountryName = "Belgium"
        Case "BZ"
        CountryName = "Belize"
        Case "BJ"
        CountryName = "Benin"
        Case "BM"
        CountryName = "Bermuda"
        Case "BT"
        CountryName = "Bhutan"
        Case "BO"
        CountryName = "Bolivia"
        Case "BA"
        CountryName = "Bosnia and Herzegovina"
        Case "BW"
        CountryName = "Botswana"
        Case "BV"
        CountryName = "Bouvet Island"
        Case "BR"
        CountryName = "Brazil"
        Case "BN"
        CountryName = "Brunei Darussalam"
        Case "BG"
        CountryName = "Bulgaria"
        Case "BF"
        CountryName = "Burkina Faso"
        Case "BI"
        CountryName = "Burundi"
        End Select
ElseIf sLetter = "C" Then
        Select Case sAbb
        Case "CH" '#
        CountryName = "Switzerland"			
        Case "CM"
        CountryName = "Cameroon"
        Case "CA"
        CountryName = "Canada"
        Case "CV"
        CountryName = "Cape Verde"
        Case "CF"
        CountryName = "Central African Republic"
        Case "CL"
        CountryName = "Chile"
        Case "CN"
        CountryName = "China"
        Case "CX"
        CountryName = "Christmas Island"
        Case "CC"
        CountryName = "Cocos (Keeling Islands)"
        Case "CO"
        CountryName = "Colombia"
        Case "CG"
        CountryName = "Congo"
        Case "CK"
        CountryName = "Cook Islands"
        Case "CR"
        CountryName = "Costa Rica"
        Case "CI"
        CountryName = "Cote D'Ivoire (Ivory Coast)"
        Case "CU"
        CountryName = "Cuba"
        Case "CY"
        CountryName = "Cyprus"
        Case "CZ"
        CountryName = "Czech Republic"
        End Select
ElseIf sLetter = "D" Then
        Select Case sAbb
        Case "DE"  '#
        CountryName = "Germany"
        Case "DZ"
        CountryName = "Algeria"			
        Case "DK"
        CountryName = "Denmark"
        Case "DJ"
        CountryName = "Djibouti"
        Case "DM"
        CountryName = "Dominica"
        Case "DO"
        CountryName = "Dominican Republic"
        End Select
ElseIf sLetter = "E" Then
        Select Case sAbb
        Case "ES"
        CountryName = "Spain"
        Case "EH"
        CountryName = "Western Sahara"		
        Case "EC"
        CountryName = "Ecuador"
        Case "EG"
        CountryName = "Egypt"
        Case "ER"
        CountryName = "Eritrea"
        Case "EE"
        CountryName = "Estonia"
        Case "ET"
        CountryName = "Ethiopia"
        End Select	
ElseIf sLetter = "F" Then
        Select Case sAbb
        Case "FK"
        CountryName = "Falkland Islands (Malvinas)"
        Case "FO"
        CountryName = "Faroe Islands"
        Case "FJ"
        CountryName = "Fiji"
        Case "FI"
        CountryName = "Finland"
        Case "FR"
        CountryName = "France"
        Case "FX"
        CountryName = "France, Metropolitan"
        Case "FM"
        CountryName = "Micronesia"
        End Select
ElseIf sLetter = "G" Then
        Select Case sAbb
        Case "GF"
        CountryName = "French Guiana"
        Case "GA"
        CountryName = "Gabon"
        Case "GM"
        CountryName = "Gambia"
        Case "GE"
        CountryName = "Georgia"
        Case "GH"
        CountryName = "Ghana"
        Case "GI"
        CountryName = "Gibraltar"
        Case "GR"
        CountryName = "Greece"
        Case "GL"
        CountryName = "Greenland"
        Case "GD"
        CountryName = "Grenada"
        Case "GS"   '#
        CountryName = "S. Georgia and S. Sandwich Isls."  
        Case "GQ"  '#
        CountryName = "Equatorial Guinea"
        Case "GP"
        CountryName = "Guadeloupe"
        Case "GU"
        CountryName = "Guam"
        Case "GT"
        CountryName = "Guatemala"
        Case "GN"
        CountryName = "Guinea"
        Case "GW"
        CountryName = "Guinea-Bissau"
        Case "GY"
        CountryName = "Guyana"
        End Select
ElseIf sLetter = "H" Then
        Select Case sAbb				
        Case "HT"
        CountryName = "Haiti"
        Case "HM"
        CountryName = "Heard and McDonald Islands"
        Case "HN"
        CountryName = "Honduras"
        Case "HK"
        CountryName = "Hong Kong"
        Case "HR"
        CountryName = "Croatia (Hrvatska)"
        Case "HU"
        CountryName = "Hungary"
        End Select
ElseIf sLetter = "I" Then
        Select Case sAbb
        Case "IS"
        CountryName = "Iceland"
        Case "IN"
        CountryName = "India"
        Case "ID"
        CountryName = "Indonesia"
        Case "IO"  '#
        CountryName = "British Indian Ocean Territory"
        Case "IR"
        CountryName = "Iran"
        Case "IQ"
        CountryName = "Iraq"
        Case "IE"
        CountryName = "Ireland"
        Case "IL"
        CountryName = "Israel"
        Case "IT"
        CountryName = "Italy"
        End Select
ElseIf sLetter = "J" Then
        Select Case sAbb	
        Case "JM"
        CountryName = "Jamaica"
        Case "JP"
        CountryName = "Japan"
        Case "JO"
        CountryName = "Jordan"
        End Select
ElseIf sLetter = "K" Then
        Select Case sAbb
        Case "KH"
        CountryName = "Cambodia"
        Case "KM"
        CountryName = "Comoros"
        Case "KY"
        CountryName = "Cayman Islands"
        Case "KN"
        CountryName = "Saint Kitts and Nevis"	
        Case "KZ"
        CountryName = "Kazakhstan"
        Case "KE"
        CountryName = "Kenya"
        Case "KI"
        CountryName = "Kiribati"
        Case "KP"
        CountryName = "Korea (North) (People's Republic)"
        Case "KR"
        CountryName = "Korea (South) (Republic)"
        Case "KW"
        CountryName = "Kuwait"
        Case "KG"
        CountryName = "Kyrgyzstan (Kyrgyz Republic)"
        End Select
ElseIf sLetter = "L" Then
        Select Case sAbb
        Case "LC" '#
        CountryName = "Saint Lucia"
        Case "LA"
        CountryName = "Laos"
        Case "LV"
        CountryName = "Latvia"
        Case "LB"
        CountryName = "Lebanon"
        Case "LK"  '#
        CountryName = "Sri Lanka"
        Case "LS"
        CountryName = "Lesotho"
        Case "LR"
        CountryName = "Liberia"
        Case "LY"
        CountryName = "Libya"
        Case "LI"
        CountryName = "Liechtenstein"
        Case "LT"
        CountryName = "Lithuania"
        Case "LU"
        CountryName = "Luxembourg"
        End Select
ElseIf sLetter = "M" Then			
        Select Case sAbb		
        Case "MO"
        CountryName = "Macau"
        Case "MK"
        CountryName = "Macedonia"
        Case "MG"
        CountryName = "Madagascar"
        Case "MW"
        CountryName = "Malawi"
        Case "MY"
        CountryName = "Malaysia"
        Case "MV"
        CountryName = "Maldives"
        Case "ML"
        CountryName = "Mali"
        Case "MT"
        CountryName = "Malta"
        Case "MH"
        CountryName = "Marshall Islands"
        Case "MQ"
        CountryName = "Martinique"
        Case "MR"
        CountryName = "Mauritania"
        Case "MU"
        CountryName = "Mauritius"
        Case "MX"
        CountryName = "Mexico"
        Case "MD"
        CountryName = "Moldova"
        Case "MC"
        CountryName = "Monaco"
        Case "MN"
        CountryName = "Mongolia"
        Case "MS"
        CountryName = "Montserrat"
        Case "MA"
        CountryName = "Morocco"
        Case "MZ"
        CountryName = "Mozambique"
        Case "MM"
        CountryName = "Myanmar"
        End Select
ElseIf sLetter = "N" Then
        Select Case sAbb						
        Case "NA"
        CountryName = "Namibia"
        Case "NR"
        CountryName = "Nauru"
        Case "NP"
        CountryName = "Nepal"
        Case "NL"
        CountryName = "Netherlands"
        Case "NT"
        CountryName = "Neutral Zone (Saudia Arabia/Iraq)"
        Case "NC"
        CountryName = "New Caledonia"
        Case "NZ"
        CountryName = "New Zealand"
        Case "NI"
        CountryName = "Nicaragua"
        Case "NE"
        CountryName = "Niger"
        Case "NG"
        CountryName = "Nigeria"
        Case "NU"
        CountryName = "Niue"
        Case "NF"
        CountryName = "Norfolk Island"
        Case "MP"
        CountryName = "Northern Mariana Islands"
        Case "NO"
        CountryName = "Norway"
        End Select
ElseIf sLetter = "O" Then
        Select Case sAbb						
        Case "OM"
        CountryName = "Oman"
        End Select
ElseIf sLetter = "P" Then
        Select Case sAbb	
        Case "PK"
        CountryName = "Pakistan"
        Case "PW"
        CountryName = "Palau"
        Case "PA"
        CountryName = "Panama"
        Case "PG"
        CountryName = "Papua New Guinea"
        Case "PY"
        CountryName = "Paraguay"
        Case "PE"
        CountryName = "Peru"
        Case "PF"
        CountryName = "French Polynesia"
        Case "PH"
        CountryName = "Philippines"
        Case "PN"
        CountryName = "Pitcairn"
        Case "PL"
        CountryName = "Poland"
        Case "PT"
        CountryName = "Portugal"
        Case "PR"
        CountryName = "Puerto Rico"
        Case "PM"  '#
        CountryName = "St. Pierre and Miquelon"		
        End Select
ElseIf sLetter = "Q" Then
        Select Case sAbb						
        Case "QA"
        CountryName = "Qatar"
        End Select
ElseIf sLetter = "R" Then
        Select Case sAbb					
        Case "RE"
        CountryName = "Reunion"
        Case "RO"
        CountryName = "Romania"
        Case "RU"
        CountryName = "Russian Federation"
        Case "RW"
        CountryName = "Rwanda"
        End Select
ElseIf sLetter = "S" Then
        Select Case sAbb						
        Case "SM"
        CountryName = "San Marino"
        Case "ST"
        CountryName = "Sao Tome and Principe"
        Case "SA"
        CountryName = "Saudi Arabia"
        Case "SN"
        CountryName = "Senegal"
        Case "SC"
        CountryName = "Seychelles"
        Case "SL"
        CountryName = "Sierra Leone"
        Case "SG"
        CountryName = "Singapore"
        Case "SK"
        CountryName = "Slovakia (Slovak Republic)"
        Case "SI"
        CountryName = "Slovenia"
        Case "SB"
        CountryName = "Solomon Islands"
        Case "SO"
        CountryName = "Somalia"
        Case "SU"
        CountryName = "Soviet Union (former)"
        Case "SH"
        CountryName = "St. Helena"
        Case "SD"
        CountryName = "Sudan"
        Case "SR"
        CountryName = "Suriname"
        Case "SJ"
        CountryName = "Svalbard and Jan Mayen Islands"
        Case "SZ"
        CountryName = "Swaziland"
        Case "SE"
        CountryName = "Sweden"				
        Case "SV" '#
        CountryName = "El Salvador"
        Case "SY"
        CountryName = "Syria"
        End Select
ElseIf sLetter = "T" Then
        Select Case sAbb
        Case "TF"
        CountryName = "French Southern Territories"
        Case "TD"
        CountryName = "Chad"
        Case "TP"
        CountryName = "East Timor"		
        Case "TW"
        CountryName = "Taiwan"
        Case "TJ"
        CountryName = "Tajikistan"
        Case "TZ"
        CountryName = "Tanzania"
        Case "TH"
        CountryName = "Thailand"
        Case "TG"
        CountryName = "Togo"
        Case "TK"
        CountryName = "Tokelau"
        Case "TO"
        CountryName = "Tonga"
        Case "TT"
        CountryName = "Trinidad and Tobago"
        Case "TN"
        CountryName = "Tunisia"
        Case "TR"
        CountryName = "Turkey"
        Case "TM"
        CountryName = "Turkmenistan"
        Case "TC"
        CountryName = "Turks and Caicos Islands"
        Case "TV"
        CountryName = "Tuvalu"
        End Select
ElseIf sLetter = "U" Then
        Select Case sAbb		
        Case "UG"
        CountryName = "Uganda"
        Case "UA"
        CountryName = "Ukraine"
        Case "UK"
        CountryName = "United Kingdom (Great Britain)"
        Case "US"
        CountryName = "United States"
        Case "UY"
        CountryName = "Uruguay"
        Case "UM"
        CountryName = "US Minor Outlying Islands"
        Case "UZ"
        CountryName = "Uzbekistan"
        End Select
ElseIf sLetter = "V" Then
        Select Case sAbb
        Case "VC"
        CountryName = "Saint Vincent and The Grenadines"		
        Case "VU"
        CountryName = "Vanuatu"
        Case "VA"
        CountryName = "Vatican City State (Holy See)"
        Case "VE"
        CountryName = "Venezuela"
        Case "VN"
        CountryName = "Viet Nam"
        Case "VG"
        CountryName = "Virgin Islands (British)"
        Case "VI"
        CountryName = "Virgin Islands (US)"
        End Select
ElseIf sLetter = "W" Then
        Select Case sAbb					
        Case "WF"
        CountryName = "Wallis and Futuna Islands"
        Case "WS"
        CountryName = "Samoa"
        End Select
ElseIf sLetter = "Y" Then
                Select Case sAbb						
        Case "YE"
        CountryName = "Yemen"
        Case "YU"
        CountryName = "Yugoslavia"
        Case "YT"
        CountryName = "Mayotte"
        End Select
ElseIf sLetter = "Z" Then
        Select Case sAbb						
        Case "ZR"
        CountryName = "Zaire"
        Case "ZM"
        CountryName = "Zambia"
        Case "ZW"
        CountryName = "Zimbabwe"
        Case "ZA"
        CountryName = "South Africa"
        End Select
End If
End Function

%>
